import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { HttpService } from '../../service/httpService/http.service';
import { LocalStorageService } from '../../service/localStorageService/local-storage.service';
import { refreshTokenUrl } from "../../urlConfig";
@Injectable({
  providedIn: 'root'
})
export class TokenService {
  // newtoken: any = {
  //   accessToken: '',
  //   refreshToken: '',
  //   role: '',
  // }
  token: any = this.localStorage.getItem("token");

  constructor(
    public http: HttpService,
    private router: Router,
    public localStorage: LocalStorageService,
  ) { }

  checkoutToken(memberUrl) {
    console.log("异步");

    // this.tokenIsNull();
    // var refreshTokenObj: any = {
    //   refreshToken: this.token.refreshToken
    // };
    // var token: any = this.localStorage.getItem("token");
    // this.http.getData(`${memberUrl}?accessToken=${this.token.accessToken}`).subscribe((event: any) => {
    // if (event.code === 202) {
    //   console.log("访问令牌过期");

    //   this.checkoutRefreshToken(refreshTokenObj, token);
    // } else if (event.code === 200) {
    //   console.log("访问令牌没过期，正常获取数据");

    // }
    // else {
    //   this.router.navigate(['/login']);
    //   return;
    // }
    // });

  }

  // // 获取新的访问令牌
  // checkoutRefreshToken(refreshTokenObj, token) {
  //   this.http.postData(refreshTokenUrl, refreshTokenObj).subscribe((event: any) => { // 当访问令牌过期时，就需要发送刷新令牌重新获取访问令牌
  //     console.log(event);

  //     if (event.code === 201) {
  //       this.newtoken.accessToken = token.accessToken;
  //       this.newtoken.refreshToken = token.refreshToken;
  //       this.newtoken.role = token.role;
  //       this.newtoken.flag = true; // 表示刷新令牌 过期 了
  //       this.localStorage.setItem('token', this.newtoken);
  //       this.router.navigate(['/login']);
  //       console.log("刷新令牌过期，需重新登陆");

  //     } else if (event.code === 200) {
  //       console.log(event);
  //       this.newtoken.refreshToken = token.refreshToken;
  //       this.newtoken.accessToken = event.result.access_token;
  //       this.newtoken.role = token.role;
  //       this.newtoken.flag = false;// 表示刷新令牌 未过期 了
  //       this.localStorage.setItem('token', this.newtoken);
  //       console.log("刷新令牌未过期，正常拿到新的访问令牌，覆盖localstorage");

  //     } else {
  //       this.router.navigate(['/login']);
  //       return;
  //     }
  //   });
  // }

  // // 检验token是否为空
  // tokenIsNull() {
  //   if (this.token === null) {
  //     this.router.navigate(['/login']);
  //     return;
  //   }
  // }

}
